var ns, toast, actionsheet, WXPhotoPicker;
var app = new Vue({
    el: '#app',
    data: {
        ID:0,
        PointID:0,
        ItemsID:0,
        CheckPoint:'',
        CheckPointIndex:'',
        CheckPointType:'',
        Contents:'',
        form: {
            ID: 0,
            StatusDesc: '',
            action: 'customerserviceprocess',
            issuggestion: 0,
            processType: 1,//1-处理2-完工3-回访
            RelationIDList: ''
        },
        writeCount:0,
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,
        type:1,//1-处理2-完工3-回访
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                ItemsID: that.ItemsID,
                action: 'getcheckprojectlist',
            }, function(succeed, data, err) {
                if (succeed) {
                    if(data.status){
                        that.CheckPoint = data.data.CheckPoint;
                        that.CheckPointIndex = data.data.CheckPointIndex;
                        that.CheckPointType = data.data.CheckPointType;
                    }else{
                        api.toast({
                            msg:'数据错了',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_savezhengai:function(){
            var that = this;
            that.do_save(1);   
        },
        writeMsg:function(){
            var that=this;
            var testVal=that.Contents.length;
            that.writeCount=testVal;
        },
        do_save: function(type) {
            var that = this;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            var options = {};
            options.ID = that.ID;
            options.PlanID = that.PointID;
            options.Contents=that.Contents;
            options.action = "savacheckproject";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    ns.toast('保存成功');
                    api.sendEvent({
                        name: 'loadservicelist',
                    });
                    if(type==1){
                        ns.openWin('qualitycontrol_frm', '新增整改工单',{
                            ComeFrom:3,
                            RelatedID:that.PointID,
                            PlanID:that.ID
                        })
                    }
                    
                    setTimeout(function() {
                        api.closeWin();
                    }, 500);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                },
                toast: true,
                toastmsg: '提交中'
            });
        },
        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.imglist.push({
                        ID: 0,
                        url: ret.data,
                        thumbPath: ret.data,
                        isupload: true
                    });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.imglist.push({
                                ID: 0,
                                url: item.path,
                                thumbPath: item.path,
                                isupload: true
                            });
                        });
                    }
                }
            });
        },
        remove_img: function (index,item) {
            var that = this;
            that.imglist.splice(index, 1);
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        // do_remove_product: function(item) {
        //     var that = this;
        //     api.confirm({
        //         title: '提示',
        //         msg: '确定删除?',
        //         buttons: ['确定', '取消']
        //     }, function(ret, err) {
        //         if (ret.buttonIndex == 1) {
        //             for (var i = 0; i < that.productlist.length; i++) {
        //                 if (that.productlist[i].ID == item.ID) {
        //                     that.productlist.splice(i, 1);
        //                 }
        //             }
        //         }
        //     });
        // },

    }
});

apiready = function() {
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    WXPhotoPicker = api.require('WXPhotoPicker');
    app.ItemsID = api.pageParam.CheckpointsID || 0;
    app.ID = api.pageParam.ID || 0;
    app.PointID = api.pageParam.PointID || 0;
    // app.form.processType=api.pageParam.type;
    // app.form.issuggestion = api.pageParam.issuggestion || 0;
    app.get_data();
    api.addEventListener({
        name: 'do_choose_ckproduct_complete'
    }, function(ret, err) {
        if (ret) {
            app.get_product_list(ret.value.list);
        }
    });
    api.addEventListener({
        name: 'dosaveservicefeedone'
    }, function(ret, err) {
        app.form.RelationIDList = ret.value.IDList;
    });
    api.addEventListener({
        name: 'reloadservicelist'
    }, function(ret, err) {
        api.closeWin();
    });

}
